<template>
	<view>
		<hjx-address :address="address"></hjx-address>

		<view class="cart_content">
			<view class="cart_title">
				<text class="iconfont icon-gouwuche">购物车</text>
			</view>
			<view class="cart_main">
				<hjx-goods v-for="(item,index) in cart" :key="index" :goods='item' :showCheck="false"
					:showNumDnly="true" @changeNum="changeNum">
				</hjx-goods>
			</view>
		</view>

		<hjx-switch :buttonText="'支付'" :cartData="cart" :showAllCheck="false">
		</hjx-switch>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cart: [],
				address: {},
			}
		},
		onShow() {
			// 从缓存获取地址数据
			this.address = uni.getStorageSync('address') || {};

			const buy = uni.getStorageSync('buy') || {};
			if (buy.goods_id) { //从立即购买跳转过来
				this.cart = [];
				this.cart.push(buy);
				uni.removeStorageSync('buy');
			} else {
				// 从缓存获取购物车数据
				this.cart = (uni.getStorageSync('cart')).filter(v => v.checked);
				// 筛选
				// this.cart = this.cart.filter(v => v.checked);
			}
		},
		methods: {
			// 修改购物车相应产品的数量
			changeNum(e) {
				let index = this.cart.findIndex(v => v.goods_id == e.goods_id);
				if (e.cartNum == 0) {
					this.cart.splice(index, 1);
				} else {
					this.cart[index].cartNum = e.cartNum;
				}
				// console.log(this.cart);
				// 更改缓存中的购物车
				uni.setStorageSync('cart', this.cart);
			}
		}
	}
</script>

<style lang="scss">
	// .cart_content {
	// 	.cart_title {
	// 		border-bottom: 1px solid #c0c0c0;
	// 		padding-top: 15rpx;
	// 		font-size: 50rpx;
	// 	}

	// 	page {
	// 		padding-bottom: 100rpx;
	// 	}
	// }
</style>